package com.mab.mall.search.controller;

import com.mab.mall.search.service.ElasticSearchService;
import com.msb.common.dto.es.SkuESModel;
import com.msb.common.exception.BizCodeEnume;
import com.msb.common.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 存储商品服务到elasticSearch中
 */
@Slf4j
@RequestMapping("/search/save")
@RestController
public class ElasticSearchSaveController {

    @Autowired
    private ElasticSearchService elasticSearchService;

    /**
     * 商品上架存储数据到ES中
     * @param skuESModelList
     * @return
     */
    @PostMapping("/product")
    public R saveProduct(@RequestBody List<SkuESModel> skuESModelList){
        Boolean b = false;
        try {
            b = elasticSearchService.saveProduct(skuESModelList);
        }catch (Exception e){
            log.error("ElasticSearch商品上架错误...");
            return R.error(BizCodeEnume.PRODUCT_UP_EXCEPTION.getCode(),BizCodeEnume.PRODUCT_UP_EXCEPTION.getMsg());
        }
        if (b){
            return R.ok();
        }else {
            return R.error(BizCodeEnume.PRODUCT_UP_EXCEPTION.getCode(),BizCodeEnume.PRODUCT_UP_EXCEPTION.getMsg());
        }
    }

}
